package com.quanquan.store.repository.marketActivity.paperPlane;

import com.quanquan.store.entity.marketActivity.paperPlane.PaperPlaneGift;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @ClassName PaperPlaneGiftRepository
 * @Description
 * @Author Owen
 * @Date 2019/9/27 0027 上午 9:35
 * @Version V1.0
 **/
public interface PaperPlaneGiftRepository extends JpaRepository<PaperPlaneGift,Long> {

    List<PaperPlaneGift> findAllByActivityIdAndDifficultyAndStatusAndStartTimeBeforeAndEndTimeAfterAndRemainTotalGreaterThan(Long activityId, Integer difficulty, Integer status, Date startTime, Date endTime, int remianTotal);

    @Transactional
    @Modifying
    @Query(value = "update oo_paper_plane_gift set remain_total = remain_total - ?2 where id = ?1 and remain_total > 0",nativeQuery = true)
    Integer substractStock(Long giftId, Integer subNum);
}
